import React, { Component } from 'react';
import {
    StyleSheet,
    Modal,
    Text,
    View,
    Image,
    TextInput,
    TouchableOpacity,
    TouchableWithoutFeedback,
    DeviceEventEmitter,
    ScrollView
} from 'react-native';

import Constant from '../Constant/Constant'
import ScreenUtils from '../Utils/ScreenUtils'
import ColorUtils from '../Utils/ColorUtils'
import ToastUtils from '../Utils/ToastUtils'
import HttpUtils from '../Utils/HttpUtils'
import RuleUtils from '../Utils/RuleUtils'
import EncryptUtils from '../Utils/EncryptUtils'
import RouterUtils from '../Utils/RouterUtils'
import StorageUtils from '../Utils/StorageUtils'
import TimeUtils from '../Utils/TimeUtils'

export default class PageAjzpHandle extends React.Component
{
    static navigationOptions = ({navigation}) => ({
        gesturesEnabled: false,//是否支持滑动返回收拾，iOS默认支持，安卓默认关闭
        headerTitle: navigation.state.params.title,
        headerStyle: {
            backgroundColor: ColorUtils.blue,
            height: ScreenUtils.statusBarHeight,
        },
        headerRight: (<Text></Text>),
        headerTintColor: ColorUtils.white,
        headerTitleStyle: {
            fontSize: ScreenUtils.Font_18,
            color: ColorUtils.white,
            fontWeight: 'normal',
            flex: 1,
            alignSelf: 'center',
            textAlign: 'center',
        }
    });

    constructor(props)
    {
        super(props);
        this.state = {
            resultType: this.props.navigation.state.params.resultType,
            caseDetail: this.props.navigation.state.params.caseDetail,
            questionDesc: '',
            zfrIds: '',
            gtzfIds: '',
            zfrDesc: '请选择执法人',
            gtzfDesc: '请选择共同执法人',
        };
    }

    componentDidMount()
    {
        RouterUtils.pushRouter('PageAjzpHandle', this.props.navigation.state.key);
    }

    componentWillUnmount()
    {
        RouterUtils.popRouter('PageAjzpHandle', this.props.navigation.state.key);
    }

    jumpSelect(title)
    {
        this.props.navigation.navigate('PageAjzpSelect',
            {
                transition: 'forVertical',
                'title': title,
                callback: ((info) =>
                {
                    if (title == '选择执法人')
                    {
                        this.setState({
                            zfrIds: info.ids,
                            zfrDesc: info.names,

                        })
                    }
                    if (title == '选择共同执法人')
                    {
                        this.setState({
                            gtzfIds: info.ids,
                            gtzfDesc: info.names,
                        })
                    }
                })
            })
    }

    handle()
    {
        try
        {
            var that = this;
            RuleUtils.checkUnSelectResult(this.state.zfrDesc, '请选择执法人');
            RuleUtils.checkUnSelectResult(this.state.gtzfDesc, '请选择共同执法人');
            RuleUtils.checkEmpty(this.state.questionDesc, '请输入处理意见');
            this.state.caseDetail.flow.flag = "ok";
            this.state.caseDetail.lawEnforcorUser = this.state.zfrIds;
            this.state.caseDetail.collaborators = this.state.gtzfIds;
            this.state.caseDetail.flow.comment = this.state.questionDesc;
            console.log('caseDetail:' + JSON.stringify(this.state.caseDetail))
            StorageUtils.get('session').then((session) =>
            {
                if (session != '' && session != null)
                {
                    HttpUtils.postJsonWithRaw(Constant.ajzpHandle, session, JSON.stringify(this.state.caseDetail), function (resultcode, data)
                    {
                        ToastUtils.toastShortBottom('案件指派成功')
                        DeviceEventEmitter.emit('ajzpOnRefresh', 'param');
                        DeviceEventEmitter.emit('homeReload', 'param');
                        that.props.navigation.goBack(RouterUtils.array[2].pageKey);
                    }, function (resultcode, reason)
                    {
                        ToastUtils.toastShortBottom('错误码:' + resultcode + ',错误原因:' + reason);
                    });
                }
            });
        }
        catch (error)
        {
            ToastUtils.toastShortBottom(error);
        }
    }

    render()
    {
        return (
            <View style={styles.container}>
                <View style={{justifyContent: 'center', alignItems: 'center', paddingLeft: 16, paddingRight: 16}}>
                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.jumpSelect('选择执法人')} style={{flexDirection: 'row', alignItems: 'center', height: 50, paddingTop: 8, paddingBottom: 8, borderBottomWidth: 1, borderBottomColor: ColorUtils.line}}>
                        <Text style={{flex: 1, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>执法人</Text>
                        <Text style={{marginRight: 16, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>{this.state.zfrDesc}</Text>
                        <Image style={{width: 9, height: 16}} source={require('../img/arrowright.png')}/>
                    </TouchableOpacity>
                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.jumpSelect('选择共同执法人')} style={{flexDirection: 'row', alignItems: 'center', height: 50, paddingTop: 8, paddingBottom: 8, borderBottomWidth: 1, borderBottomColor: ColorUtils.line}}>
                        <Text style={{flex: 1, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>共同执法</Text>
                        <Text style={{marginRight: 16, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>{this.state.gtzfDesc}</Text>

                        <Image style={{width: 9, height: 16}} source={require('../img/arrowright.png')}/>
                    </TouchableOpacity>
                    <View style={{flexDirection: 'row', height: 150, paddingTop: 8, paddingBottom: 8}}>
                        <View style={{width: 80, height: 50, flexDirection: 'row', alignItems: 'center',}}>
                            <Text style={{fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>处理意见</Text>
                        </View>
                        <View style={{flex: 1, borderColor: ColorUtils.line, borderWidth: 1, borderRadius: 10, padding: 8}}>
                            <TextInput value={this.state.questionDesc} underlineColorAndroid='transparent'
                                       keyboardType={'default'} // 默认键盘类型
                                       autoCapitalize='none' // 取消首字母大写
                                       placeholder='请输入处理意见'
                                       placeholderTextColor={ColorUtils.gray}
                                       multiline={true}
                                       onChangeText={(text) => this.setState({questionDesc: text})}
                                       style={{fontSize: ScreenUtils.Font_12, color: ColorUtils.black, width: "100%", height: "100%", textAlignVertical: 'top'}}/>
                        </View>
                    </View>
                </View>
                <View style={{margin: 32}}>
                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.handle()}>
                        <View style={{justifyContent: 'center', alignItems: 'center', height: 44, padding: 4, borderRadius: 6, backgroundColor: ColorUtils.blue}}>
                            <Text style={{color: ColorUtils.white, fontSize: ScreenUtils.Font_14}}>
                                指派
                            </Text>
                        </View>
                    </TouchableOpacity>
                </View>
            </View>
        );
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: ColorUtils.white,
    },
    tabBarUnderlineStyle: {
        height: 2,
        minWidth: Math.floor(ScreenUtils.width * 0.8 / 8),
        backgroundColor: ColorUtils.blue,
    },
    tabBarTextStyle: {
        fontSize: ScreenUtils.Font_14,
        fontWeight: 'normal'
    }
});


